﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class GetID
    {
        public static Entities.GetID SelectID(Entities.GetID id)
        {
            Entities.GetID list = new Entities.GetID();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.LayID.sp_LayID, cn);
                cmd.Parameters.Add(Common.Constants.LayID.tableName, SqlDbType.NVarChar).Value = id.TableName;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    list.ColumnName = dr[0].ToString();
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }


        public static Entities.GetID sp_getIDHoaDonBanLe(Entities.GetID id)
        {
            Entities.GetID list = new Entities.GetID();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.LayID.sp_getIDHoaDonBanLe, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.LayID.MaKho, SqlDbType.VarChar).Value = id.MaKho;
                cmd.Parameters.Add(Common.Constants.LayID.Ngay, SqlDbType.VarChar).Value = id.Ngay;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    list.ColumnName = dr[0].ToString();
                }
            }
            catch
            {}
            return list;
        }
       static SqlConnection cn;
       static SqlCommand cmd;

        public static Entities.GetID sp_getID_BanLe(Entities.GetID id)
        {
            SqlDataReader dr;
            Entities.GetID list = new Entities.GetID();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.LayID.sp_getID_BanLe, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.LayID.MaHDBanLe, SqlDbType.VarChar).Value = id.MaHoaDon;
                cmd.Parameters.Add(Common.Constants.LayID.MaKho, SqlDbType.VarChar).Value = id.MaKho;
                cmd.Parameters.Add(Common.Constants.LayID.Ngay, SqlDbType.VarChar).Value = id.Ngay;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    list.ColumnName = dr[0].ToString();
                }
            }
            catch
            { }
            return list;
        }

        /// <summary>
        /// trả lại nhà cung cấp
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Entities.GetID sp_getIDTraLaiNCC(Entities.GetID id)
        {
            SqlDataReader dr;
            Entities.GetID list = new Entities.GetID();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.TraLaiNCC.getID, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.LayID.MaKho, SqlDbType.VarChar).Value = id.MaKho;
                cmd.Parameters.Add(Common.Constants.LayID.Ngay, SqlDbType.VarChar).Value = id.Ngay;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    list.ColumnName = dr[0].ToString();
                }
                cmd.Connection.Dispose();
                cn.Close();
                return list;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                return null;
            }

        }
        public static Entities.GetID sp_getIDHDBB(Entities.GetID id)
        {
            SqlDataReader dr;
            Entities.GetID list = new Entities.GetID();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.HDBanBuon.GetID, cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(Common.Constants.LayID.MaKho, SqlDbType.VarChar).Value = id.MaKho;
                cmd.Parameters.Add(Common.Constants.LayID.Ngay, SqlDbType.DateTime).Value = id.Ngay;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    list.ColumnName = dr[0].ToString();
                }
                cmd.Connection.Dispose();
                cn.Close();
                return list;
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                return null;
            }

        }
    }
}
#endregion
